package com.chinawlx.wlxteacher.widget.photoview;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Hacky fix for Issue #4 and
 * http://code.google.com/p/android/issues/detail?id=18990
 * <p/>
 * ScaleGestureDetector seems to mess up the touch events, which means that
 * ViewGroups which make use of onInterceptTouchEvent throw a lot of
 * IllegalArgumentException: pointerIndex out of range.
 * <p/>
 * There's not much I can do in my code for now, but we can mask the result by
 * just catching the problem and ignoring it.
 *
 * @author Chris Banes
 */
public class HackyViewPager extends ViewPager {
	
    public HackyViewPager(Context context) {
        super(context);
    }

    public HackyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 避免与ViewPager冲突报错
     * 错误原因：PhotoView和viewPager冲突问题

     原因理解：

     多次触发触摸屏事件，导致对同一个事件处理的消息过多，当第一个消息已处理完事件并销毁事件时，由于该事件已销毁但还没来得及通知销毁第二个同样的消息，当主线程执行第二个消息时，由于获取不了该事件，所以抛出异常。
     解决：用try..catch..包围onTouch() 和 onInterceptTouchEvent()事件处理。
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
		try {
			return super.onInterceptTouchEvent(ev);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
			return false;
		}
    }
}
